package payment.simulator.institution.tx.auth;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import payment.api.tx.auth.Tx1702Request;


public class Tx1702 extends HttpServlet {

	private static final long serialVersionUID = 1101894385342342734L;

	@Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // 1.取得参数
            String institutionID = request.getParameter("InstitutionID");
            String txSN = request.getParameter("TxSN");
            String accountName = request.getParameter("AccountName");
            String accountNumber = request.getParameter("AccountNumber");
            String identificationType = request.getParameter("IdentificationType");
            String identificationNumber = request.getParameter("IdentificationNumber");
            String phoneNumber = request.getParameter("PhoneNumber");
            String validDate = request.getParameter("ValidDate");
            String cVN2 = request.getParameter("CVN2");
            String payerID = request.getParameter("PayerID");
            String payerName = request.getParameter("PayerName");
            String usage = request.getParameter("Usage");
            String remark = request.getParameter("Remark");

            // 2.创建交易请求对象
            Tx1702Request tx1702Request = new Tx1702Request();
            tx1702Request.setInstitutionID(institutionID);
            tx1702Request.setTxSN(txSN);
            tx1702Request.setAccountName(accountName);
            tx1702Request.setAccountNumber(accountNumber);
            tx1702Request.setIdentificationType(identificationType);
            tx1702Request.setIdentificationNumber(identificationNumber);
            tx1702Request.setPhoneNumber(phoneNumber);
            tx1702Request.setValidDate(validDate);
            tx1702Request.setCVN2(cVN2);
            tx1702Request.setPayerID(payerID);
            tx1702Request.setPayerName(payerName);
            tx1702Request.setUsage(usage);
            tx1702Request.setRemark(remark);

            // 3.执行报文处理
            tx1702Request.process();

            // 4.将参数放置到request对象
            request.setAttribute("plainText", tx1702Request.getRequestPlainText());
            request.setAttribute("message", tx1702Request.getRequestMessage());
            request.setAttribute("signature", tx1702Request.getRequestSignature());
            request.setAttribute("txCode", "1702");
            request.setAttribute("txName", "后台预授权发送短信");
            request.setAttribute("action", request.getContextPath() + "/SendMessage");

            // 5.转向Request.jsp页面
            request.getRequestDispatcher("/Request.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            processException(request, response, e.getMessage());
        }
    }

    private void processException(HttpServletRequest request, HttpServletResponse response, String errorMessage) {
        try {
            request.setAttribute("errorMessage", errorMessage);
            request.getRequestDispatcher("/ErrorPage.jsp").forward(request, response);
        } catch (Exception e) {
        }
    }

}
